<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\system\service;

use app\common\service\BaseService;
use think\App;

class RoleMenuService extends BaseService
{
    /**
     * 初始化
     * @param App $app
     */
    public function __construct(App $app)
    {
        $this->config['isDelete'] = false;
        parent::__construct($app, '角色菜单', 'role_menu');
    }

    /**
     * 保存
     * @author 贾二小
     * @since 2022/2/20
     */
    public function permissionSaving(int $roleId,array $menuId) :void
    {
        $this->transaction(function () use ($menuId,$roleId){
            //删除数据
            $map = [['roleId','=',$roleId]];
            $this->deleteByMap($map);
            //新增数据
            $menuRoleData = array_map(function ($item) use ($roleId){return ['menuId'=>$item,'roleId'=>$roleId];},$menuId);
            $this->insertAll($menuRoleData);
        });
    }
}